﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;

namespace TTBox
{
    static class Program
    {

        static public bool m_bRun = true;
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [MTAThread]
        static void Main()
        {
            m_bRun = true;

            //while (m_bRun)
            {
                try
                {
                    Application.Run(new Form_Main());
                }
                catch (Exception ex)
                {


                    TextWriter tw = null;
                    try
                    {

                        DirectoryInfo di = new DirectoryInfo(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.FullyQualifiedName) + "\\Logs");
                        if (!di.Exists)
                        {
                            di.Create();
                        }

                        DateTime dt = System.DateTime.Now;
                        string strFileName = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.FullyQualifiedName) + "\\Logs\\Log_" + dt.ToString("yyMMdd") + ".txt";
                        tw = new StreamWriter(strFileName, true);


                        string strLog = LogManager.getInstance().getLog();
                        while (strLog != "")
                        {
                            tw.WriteLine(strLog);
                            strLog = LogManager.getInstance().getLog();
                        }


                        try
                        {
                            for (int i = 0; i < 15; i++)
                            {
                                dt = dt.AddDays(i * -1);
                                strFileName = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.FullyQualifiedName) + "\\Logs\\Log_" + dt.ToString("yyMMdd") + ".txt";
                                FileInfo fi = new FileInfo(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.FullyQualifiedName) + "\\Logs\\Log_" + dt.ToString("yyMMdd") + ".txt");
                                if (fi.Exists)
                                {
                                    fi.Delete();
                                }
                            }

                        }
                        catch (Exception exFi)
                        {
                        }

                        tw.WriteLine(ex.Message);
                        tw.WriteLine(ex.StackTrace);


                    }
                    catch (Exception ex2)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    finally
                    {
                        if (tw != null)
                            tw.Close();
                    }
                    /*
                    if (Program.IsDebug)
                    {
                        int nLen = ex.Message.Length;
                        if (nLen > 100)
                            nLen = 100;
                        MessageBox.Show(ex.Message.Substring(0, nLen));
                        nLen = ex.StackTrace.Length;
                        if (nLen > 100)
                            nLen = 100;
                        MessageBox.Show(ex.StackTrace.Substring(0, nLen));
                    }
                     */
                }
            }
        }



    }

}